package com.ruoyi.cloudpc.controller;

import com.ruoyi.cloudpc.domain.CloudPromotion;
import com.ruoyi.cloudpc.domain.CloudWithdrawal;
import com.ruoyi.cloudpc.service.CloudPromotionService;
import com.ruoyi.cloudpc.service.ICloudWithdrawalService;
import com.ruoyi.cloudpc.utils.SequenceGenerator;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.PicUrlUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.uuid.IdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;

/**
 * 推广业务相关
 *
 * @author Ten
 */
@RestController
@RequestMapping("/cloudpc/promotion")
public class CloudPromotionController extends BaseController {
    @Autowired
    private SequenceGenerator sequenceGenerator;

    @Autowired
    private CloudPromotionService cloudPromotionService;

    @Autowired
    private ICloudWithdrawalService cloudWithdrawalService;
    @Autowired
    private PicUrlUtil picUrlUtil;

    /**
     * 判断当前用户是否可以推广
     *
     * @return 结果
     */
    @GetMapping(value = "/hasAuth")
    public AjaxResult hasAuth() {
        return AjaxResult.success(cloudPromotionService.hasAuth());
    }

    /**
     * 保存推广人信息
     *
     * @param promoter 推广人的username
     * @return 结果
     */
    @PostMapping("/promoter/{promoter}")
    public AjaxResult savePromoter(@PathVariable(value = "promoter") String promoter) {
        return AjaxResult.success(cloudPromotionService.savePromoter(promoter));
    }

    /**
     * 查询推广列表
     */
    @GetMapping("/list")
    public TableDataInfo list(CloudPromotion cloudPromotion) {
        startPage();
        List<CloudPromotion> list = cloudPromotionService.selectCloudPromotionList(cloudPromotion);
        return getDataTable(list);
    }

    /**
     * 导出推广列表
     */
    @Log(title = "推广", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CloudPromotion cloudPromotion) {
        List<CloudPromotion> list = cloudPromotionService.selectCloudPromotionList(cloudPromotion);
        ExcelUtil<CloudPromotion> util = new ExcelUtil<CloudPromotion>(CloudPromotion.class);
        util.exportExcel(response, list, "推广数据");
    }

    /**
     * 获取推广详细信息
     */
    @GetMapping(value = "/{fromUserName}")
    public AjaxResult getInfo(@PathVariable("fromUserName") String fromUserName) {
        return success(cloudPromotionService.selectCloudPromotionByFromUserName(fromUserName));
    }

    /**
     * 新增推广
     */
    @Log(title = "推广", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CloudPromotion cloudPromotion) {
        return toAjax(cloudPromotionService.insertCloudPromotion(cloudPromotion));
    }

    /**
     * 修改推广
     */
    @Log(title = "推广", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CloudPromotion cloudPromotion) {
        return toAjax(cloudPromotionService.updateCloudPromotion(cloudPromotion));
    }

//    /**
//     * 删除推广
//     */
//    @Log(title = "推广", businessType = BusinessType.DELETE)
//    @DeleteMapping("/{fromUserNames}")
//    public AjaxResult remove(@PathVariable String[] fromUserNames) {
//        return toAjax(cloudPromotionService.deleteCloudPromotionByFromUserNames(fromUserNames));
//    }

    /**
     * 推广详情
     *  当前佣金
     *  推广人数
     *  累计收益
     * @return
     */
    @GetMapping(value = "/detail")
    public AjaxResult detail() {
        return success(cloudPromotionService.detail());
    }

    /**
     * 提现
     * @param cloudWithdrawal .money 提现金额
     * @return
     */
    @PostMapping(value = "/withdrawal")
    public AjaxResult withdrawal(@RequestBody CloudWithdrawal cloudWithdrawal) {
        if (cloudWithdrawal.getMoney() == null) {
            throw new RuntimeException("未传入提现金额");
        }

        BigDecimal current = (BigDecimal) cloudPromotionService.detail().get("current");
        if (current.compareTo(cloudWithdrawal.getMoney()) < 0) {
            // 提现金额比可提金额高，不给操作
            throw new RuntimeException("暂无足够可提现金额，请刷新页面后重试");
        }

        cloudWithdrawal.setId(IdUtils.fastUUID());
        cloudWithdrawal.setOrderNo(sequenceGenerator.generateNumber());
        cloudWithdrawal.setMoney(cloudWithdrawal.getMoney());
        return success(cloudWithdrawalService.insertCloudWithdrawal(cloudWithdrawal));
    }


    @GetMapping("code")
    public void aaa() {
        System.out.println("sequenceGenerator.generateNumber() = " + sequenceGenerator.generateNumber());
//        @Autowired
        picUrlUtil.handlePicUrl("");
//        private PicUrlUtil picUrlUtil;

    }
}

